package com.icss.train_sys_back.controller.admin.train_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.train_manage.RailwayBureauEntity;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.entity.train_manage.StationEntity;
import com.icss.train_sys_back.service.train_manage.StationService;
import com.icss.train_sys_back.service.train_manage.RailwayBureauManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wk
 * @desc
 * @date 2025/9/23 14:04
 */
@Slf4j
@RestController
@RequestMapping("/admin/station_manage")
public class StationManageController {

    @Autowired
    private StationService stationService;

    @Autowired
    private RailwayBureauManageService trainManageService;


    // 获取车站列表（分页）
    @GetMapping("/getList")
    public ResponseResult<PageInfo<StationEntity>> getList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String stationName,
            @RequestParam(required = false) String province) {

        log.info("车站列表分页+条件检索： page={},pageSize={},stationName={},province={}", page, pageSize, stationName, province);

        PageHelper.startPage(page, pageSize);
        List<StationEntity> stationList = stationService.findStationList(stationName, province);
        log.info("stationList  = {}", stationList);
        PageInfo<StationEntity> pageInfo = new PageInfo<>(stationList);

        return ResponseResult.success(pageInfo);
    }

    // 保存车站信息（新增或修改）
    @PostMapping("/save")
    public ResponseResult save(@RequestBody StationEntity stationEntity) {
        Integer stationId = stationEntity.getStationId();
        //插入车站
        if (stationId == null) {
            log.info("保存车站={}", stationEntity);
            boolean res = stationService.save(stationEntity);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        } else {
            //更新车站
            log.info("更新车站={}", stationEntity);
            boolean res = stationService.update(stationEntity);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        }
    }

    // 删除车站
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        log.info("删除id = {} 的车站", id);
        boolean res = stationService.delete(id);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    // 获取所有铁路局列表
    @GetMapping("/getAllBureauList")
    public ResponseResult getAllBureauList() {
        List<RailwayBureauEntity> bureauList = trainManageService.getAllBureauList();
        return ResponseResult.success(bureauList);
    }

}
